package com.googlecode.gwtbb.client.only.widget.display.forum;

import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtbb.client.only.ClientOnlyMessages;
import com.googlecode.gwtbb.client.only.GwtbbCmess;
import com.googlecode.gwtbb.client.only.glue.TokenOwner;
import com.googlecode.gwtbb.client.only.glue.events.forum.ForumEventListener;
import com.googlecode.gwtbb.client.only.glue.events.user.UserAuthenticationEventListener;
import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.remote.NiceCallback;
import com.googlecode.gwtbb.client.only.remote.forum.ForumServiceProxy;
import com.googlecode.gwtbb.client.only.widget.SessionWidget;
import com.googlecode.gwtbb.client.only.widget.basic.ModuleHeader;
import com.googlecode.gwtbb.client.only.widget.form.FormWidget;
import com.googlecode.gwtbb.client.only.widget.form.SingletonFormDialogLink;
import com.googlecode.gwtbb.client.only.widget.form.forum.CreateForumForm;
import com.googlecode.gwtbb.client.shared.entity.forum.Forum;
import com.googlecode.gwtbb.client.shared.entity.forum.Post;
import com.googlecode.gwtbb.client.shared.entity.user.User;
import com.googlecode.gwtbb.client.shared.remote.forum.ForumEvent;
import com.googlecode.gwtbb.client.shared.remote.user.UserEvent;

import java.util.List;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since May 4, 2008
 */
public class ForumList extends SessionWidget
	implements TokenOwner, UserAuthenticationEventListener, ForumEventListener {
	protected static final ClientOnlyMessages c = GwtbbCmess.instance();
	private VerticalPanel mainPanel;
	private SimplePanel forumTableWrapper;
	private FlexTable forumTable;
	private HorizontalPanel header;

	public ForumList(ClientSession session) {
		super(session);

		mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		mainPanel.setWidth("100%");
		ModuleHeader moduleHeader = new ModuleHeader();
		mainPanel.add(moduleHeader);

		header = new HorizontalPanel();
		moduleHeader.setHeaderLeft(header);

		Label headerLabel = new Label(c.moduleHeaderForums());
		header.add(headerLabel);
		headerLabel.setStylePrimaryName("gwtbb-header");

		forumTableWrapper = new SimplePanel();
		mainPanel.add(forumTableWrapper);
		forumTableWrapper.setWidth("100%");
		resetForumTable();

		refreshForumList();
	}

	public void onCreate(ForumEvent forumEvent) {
		refreshForumList();
	}

	public void onLogin(UserEvent userEvent) {
		User user = userEvent.getUser();
		handleLogout(); // handles logging in if we're already logged-in

		if (user.isGwtbbAdmin()) {
			HorizontalPanel panel = new HorizontalPanel();
			panel.add(new HTML("("));
			panel.add(new SingletonFormDialogLink(GwtbbCmess.instance().createForumLink()) {
				public FormWidget createForm() {
					return new CreateForumForm(getSession());
				}
			});
			panel.add(new HTML(")"));
			panel.setStylePrimaryName("gwtbb-navUnit");
			header.add(panel);
		}
	}

	public void onLogout(UserEvent userEvent) {
		handleLogout();
	}

	public boolean owns(String token) {
		return true;
	}

	public void serve(String token) {
		// todo do we need to do something here?
	}

	private void formatRow(FlexTable.FlexCellFormatter fcf, int row) {
		fcf.setStylePrimaryName(row, 0, "gwtbb-spreadsheet-cell");
		fcf.setStylePrimaryName(row, 1, "gwtbb-spreadsheet-cell");
		fcf.setStylePrimaryName(row, 2, "gwtbb-spreadsheet-cell");
		fcf.setStylePrimaryName(row, 3, "gwtbb-spreadsheet-cell");
	}

	private void handleLogout() {
		while (header.getWidgetCount() > 1)
			header.remove(1);
	}

	private void refreshForumList() {
		ForumServiceProxy.instance().getForumsWithInfo(new NiceCallback<List<Forum>>() {
			public void handle(List<Forum> forums) {
				resetForumTable();
				FlexTable.FlexCellFormatter fcf = forumTable.getFlexCellFormatter();

				// to counter setting the colspan when there is no forums
				fcf.setColSpan(1, 0, 1);

				forumTable.setWidget(0, 0, new Label(c.fieldHeaderForum()));
				forumTable.setWidget(0, 1, new Label(c.fieldHeaderNumThreads()));
				forumTable.setWidget(0, 2, new Label(c.fieldHeaderNumPosts()));
				forumTable.setWidget(0, 3, new Label(c.fieldHeaderLastPost()));
				formatRow(fcf, 0);
				if (forums != null && !forums.isEmpty()) {
					int row = 1;
					for (Forum forum : forums) {
						VerticalPanel forumPanel = new VerticalPanel();
						forumPanel.add(new ForumLink(forum));
						Label descriptionLabel = new Label(forum.getDescription());
						descriptionLabel.setStyleName("gwtbb-subtle");
						forumPanel.add(descriptionLabel);

						forumTable.setWidget(row, 0, forumPanel);
						Forum.Info forumInfo = forum.getInfo();
						forumTable.setWidget(row, 1, new Label(String.valueOf(forumInfo.getNumThreads())));
						forumTable.setWidget(row, 2, new Label(String.valueOf(forumInfo.getNumPosts())));
						Post lastPost = forumInfo.getLastPost();

						Widget lastPostWidget;
						if (lastPost != null)
							lastPostWidget = new PostSummaryWithSubject(lastPost);
						else
							lastPostWidget = new Label();
						forumTable.setWidget(row, 3, lastPostWidget);

						formatRow(fcf, row);
						row++;
					}
				} else {
					forumTable.setWidget(1, 0, new Label(c.noForums()));
					fcf.setColSpan(1, 0, 4);
					fcf.setStylePrimaryName(1, 0, "gwtbb-spreadsheet-cell");
				}
			}
		});
	}

	private void resetForumTable() {
		forumTable = new FlexTable();
		forumTableWrapper.setWidget(forumTable);
		forumTable.setWidth("100%");
		forumTable.setStylePrimaryName("gwtbb-spreadsheet");
		forumTable.setCellPadding(1);
	}
}
